﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gollie.Mail;
using MySQL.Db;

namespace Gollie.Controls.Admin
{
	public partial class SendMail : AdminRequiredBaseUserControl
	{

		protected override void OnPostBack(object sender, EventArgs e)
		{
			string body = this.tbBody.Text;

			string rec = this.rblReceivers.SelectedValue;
			string subject = this.tbSubject.Text;
			if (subject.Length < 4)
			{
				this.tbSubject.BackColor = System.Drawing.Color.FromArgb(255, 0, 0);
				return;
			}

			bool includeConsumers = (rec == "c" || rec == "b");
			bool includeVendors = (rec == "v" || rec == "b");

			ResultSet resultSet = Database.Queries.GetUsersForMail(includeConsumers, includeVendors);

			if (resultSet.Count > 0)
			{
				BulkMail mails = new BulkMail(body, subject);

				for (int i = 0; i < resultSet.Count; i++)
				{
					string email = resultSet.GetString(i, "email");
					mails.Add(email);
				}

				mails.DoQueue();
			}
		}
	}
}